const {getDB} = require('../db/nedbService');
const cookieParser = require('cookie-parser');

var userDb = null;

let handleLogin = function (req,res) {
    if (userDb==null) {
        userDb = getDB('user');
    }
    //获取登录描述
    let loginInfo =req.body;
    console.log(loginInfo);
    if (loginInfo===undefined)return;
    dealIt(req,res,loginInfo);
};

let dealIt = function(req,res,loginInfo){
  /*一、登录
  *     1、已经登录
  *     2、数据库比对
  *        a、存在，成功并且登记session
  *        b、不存在，拒绝
  * 二、注册
  *     1、已经存在该用户，拒绝
  *     2、不存在，注册
  * */
  let loginReturn = {
    status:'',//only success/failure/warning allowed here
    info:'',
  };
  if (loginInfo.type==='登录'){
      let {hello} = req.cookies;
      let sessionId = cookieParser.signedCookie(hello,'localhost');
      userDb.find({sessionId: sessionId},function (err,doc) {
          if (sessionId!==undefined && doc.length===0){
              loginReturn.status = 'warning';
              loginReturn.info = '请重新登录';
              res.send(loginReturn);
          }else{
              userDb.find({username:loginInfo.username},function (err,doc) {
                  if (doc.length>0&&doc[0].password===loginInfo.password) {
                      loginReturn.status = 'success';
                      loginReturn.info = '登录成功';
                      let sessionId = req.sessionID;
                      res.cookie('hello',sessionId);
                      userDb.update({username:loginInfo.username},{$set:{sessionId:sessionId}},function () {
                          res.send(loginReturn);
                      })
                  }else{
                      loginReturn.status = 'failure';
                      loginReturn.info = '用户名或者密码不准确';
                      res.send(loginReturn);
                  }
              });
          }
      })
  } else if (loginInfo.type==='注册'){
      userDb.find({username:loginInfo.username},function (err,doc) {
            if (doc.length>0){
                loginReturn.status = 'failure';
                loginReturn.info = '该用户名已经存在';
                res.send(loginReturn);
            } else{
                loginReturn.status = 'success';
                loginReturn.info = '注册成功';
                let user = {
                    username:loginInfo.username,
                    password:loginInfo.password,
                    sessionId:"",
                };
                userDb.insert(user,function (err) {
                    res.send(loginReturn);
                });
            }
      })
  } else{
      loginReturn.status = 'failure';
      loginReturn.info = '未知命令';
      res.send(loginReturn);
  }
};

let forgetPassword = function(req,res){
    let username = req.req.body.username;
    let re = {
        status:false,
        password: '',
    };
    if (userDb==null)userDb=getDB('user');
    userDb.find({username:username},function (err,doc) {
        if (doc.length>0){
            re.status=true;
            re.password=doc[0].password;
        }
        res.send(re);
    })
};

module.exports.forgetPassword=forgetPassword;

module.exports.handleLogin = handleLogin;
